//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmBatesOp.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Processes/BatesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmQuantoHelper.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpComposite.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (boost::shared_ptr<QuantLib::FdmBatesOp>& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
#ifdef HANDLE
	_phFdmBatesOp = NULL;
#endif
	_ppFdmBatesOp = &childNative;
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBatesOp));
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (QuantLib::FdmBatesOp& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
#ifdef HANDLE
	_phFdmBatesOp = NULL;
#endif
	_ppFdmBatesOp = new boost::shared_ptr<QuantLib::FdmBatesOp> (&childNative);
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBatesOp));
    _FdmBatesOpOwner = owner;
    _FdmLinearOpCompositeOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (CFdmBatesOp^ copy) : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
#ifdef HANDLE
	_phFdmBatesOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBatesOp = new boost::shared_ptr<QuantLib::FdmBatesOp> (copy->GetShared());
        _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBatesOp));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (PLATFORM::Type^ t) : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
#ifdef HANDLE
	_phFdmBatesOp = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBatesOp::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (QuantLib::Handle<QuantLib::FdmBatesOp>& childNative, Object^ owner)  : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
	_phFdmBatesOp = &childNative;
	_ppFdmBatesOp = &static_cast<boost::shared_ptr<QuantLib::FdmBatesOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBatesOp));
    _FdmBatesOpOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (QuantLib::Handle<QuantLib::FdmBatesOp> childNative)  : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
	_phFdmBatesOp = &childNative;
	_ppFdmBatesOp = &static_cast<boost::shared_ptr<QuantLib::FdmBatesOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBatesOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::CFdmBatesOp (QuantLib::FdmBatesOp childNative)  : CFdmLinearOpComposite(CFdmBatesOp::typeid)
{
#ifdef HANDLE
	_phFdmBatesOp = NULL;
#endif
	_ppFdmBatesOp = new boost::shared_ptr<QuantLib::FdmBatesOp> (new QuantLib::FdmBatesOp (childNative));
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmBatesOp));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::~CFdmBatesOp ()
{
    if (_ppFdmBatesOp != NULL)
    {
	    delete _ppFdmBatesOp;
        _ppFdmBatesOp = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::!CFdmBatesOp ()
{
    if (_ppFdmBatesOp != NULL)
    {
	    delete _ppFdmBatesOp;
    }
}
QuantLib::FdmBatesOp& Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::GetReference ()
{
    if (_ppFdmBatesOp == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmBatesOp;
}
boost::shared_ptr<QuantLib::FdmBatesOp>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::GetShared ()
{
    if (_ppFdmBatesOp == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmBatesOp;
}
QuantLib::FdmBatesOp* Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::GetPointer ()
{
    if (_ppFdmBatesOp == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmBatesOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBatesOp>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::GetHandle ()
{
	if (_phFdmBatesOp == NULL)
	{
		_phFdmBatesOp = new Handle<QuantLib::FdmBatesOp> (*_ppFdmBatesOp);
	}
	return *_phFdmBatesOp;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::HasNative () 
{
	return (_ppFdmBatesOp != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::IFdmBatesOp^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1); //a
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2); //a
    	(*_ppFdmBatesOp)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmBatesOp::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmBatesOp)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

